package boundarys;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import common.CheckBoxObject;
import common.CheckBoxJList;
import controllers.initiateSaleController;
import entitys.Customer;
import entitys.MarketingPattern;
import entitys.Product;
import entitys.Reply;
import enums.Command;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.JPanel;
import java.awt.SystemColor;

public class InitiateSaleGUI extends AbstractGUI {

	private JFrame frmCcrminitiateSale;
	
	private ArrayList arr;
	private ArrayList<MarketingPattern> patternsArr;
	private ArrayList<Customer> customersArr;
	private JComboBox salesPersonCombo,designPatternCombo;
	private ArrayList<ArrayList<CheckBoxObject>> salesPersonToCustomer;
	private int selectedSalesPerson=0;
	private boolean alreadyInitialized=false;
	// check box list vars
	private ArrayList<CheckBoxJList> Custs;
	private ArrayList<DefaultListModel> defModel;
	private ArrayList<JScrollPane> custScrollLists;
	
	public InitiateSaleGUI() {
		initialize();
		
		salesPersonToCustomer=new ArrayList<ArrayList<CheckBoxObject>>();
		custScrollLists=new ArrayList<JScrollPane>();
		Custs=new ArrayList<CheckBoxJList>();
		defModel=new ArrayList<DefaultListModel>();
		
		
		initGUI(frmCcrminitiateSale);
		frmCcrminitiateSale.getContentPane().setLayout(null);
		
		
		
		salesPersonCombo = new JComboBox();
		salesPersonCombo.setBounds(48, 291, 271, 20);
		frmCcrminitiateSale.getContentPane().add(salesPersonCombo);
		initiateSaleController.getSalesPersons();
		salesPersonCombo.addItemListener(new ItemListener() {		     
			@Override
			public void itemStateChanged(ItemEvent event) {
				if (event.getStateChange() == ItemEvent.SELECTED && alreadyInitialized) {
			          Object item = event.getItem();
			          
			          custScrollLists.get(selectedSalesPerson).setVisible(false);
			          /*
			          for (int i=0;i<salesPersonToCustomer.get(selectedSalesPerson).size();i++)
			          {
			        	  if (salesPersonToCustomer.get(selectedSalesPerson).get(i).getSlected()==1)
			        		  System.out.println(salesPersonToCustomer.get(selectedSalesPerson).get(i).getName());
			          }
			          
			          */
			          
			          selectedSalesPerson=salesPersonCombo.getSelectedIndex();
			          
			          custScrollLists.get(selectedSalesPerson).setVisible(true);
			          
			       }
				
				
			}
	    });
		
		
		
		
		
		
		designPatternCombo = new JComboBox();
		designPatternCombo.setBounds(48, 52, 271, 20);
		frmCcrminitiateSale.getContentPane().add(designPatternCombo);
		initiateSaleController.getMarketingPatterns();
		designPatternCombo.addItemListener(new ItemListener() {		     
				@Override
				public void itemStateChanged(ItemEvent event) {
					if (event.getStateChange() == ItemEvent.SELECTED) {
				          Object item = event.getItem();
				          initiateSaleController.getPatternedClients(designPatternCombo.getSelectedItem().toString());
				       }
					
				}
		    });
		JLabel lblPickADisign = new JLabel("Pick a design pattern");
		lblPickADisign.setBounds(129, 27, 135, 14);
		frmCcrminitiateSale.getContentPane().add(lblPickADisign);
		
		
		
		
		
		JLabel lblPickASales = new JLabel("Pick a Salesperson");
		lblPickASales.setBounds(140, 266, 135, 14);
		frmCcrminitiateSale.getContentPane().add(lblPickASales);
		
		JButton btnInitiateSale = new JButton("Create New Sale");
		btnInitiateSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				initiateSaleController.initiateNewSale(salesPersonToCustomer, designPatternCombo.getSelectedItem().toString());
				JOptionPane.showMessageDialog(frmCcrminitiateSale, "Sale was created!", "ccrm->Initiate Sale", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnInitiateSale.setBounds(103, 344, 146, 30);
		frmCcrminitiateSale.getContentPane().add(btnInitiateSale);
		
		initiateSaleController.getPatternedClients();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCcrminitiateSale = new JFrame();
		frmCcrminitiateSale.setTitle("CCRM>Initiate Sale");
		frmCcrminitiateSale.setResizable(false);
		frmCcrminitiateSale.setBounds(100, 100, 384, 413);
		frmCcrminitiateSale.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmCcrminitiateSale.setVisible(true);
	}

	@Override
	public void getReply(Object r) {
		// TODO Auto-generated method stub
		
		if(((Reply)r).command.equals(Command.GET_SALE_PERSONS)) //if GET_PRODUCTS reply is received
		{
			arr =(ArrayList<String>) ((Reply)r).result;
			
			salesPersonCombo.removeAllItems();
			
			for(int i=0;i<arr.size();i++)
			{
				salesPersonCombo.addItem(arr.get(i).toString());
				salesPersonToCustomer.add(new ArrayList<CheckBoxObject>());
				
				Custs.add(new CheckBoxJList(salesPersonToCustomer.get(i)));
				defModel.add(new DefaultListModel());
				Custs.get(i).setModel (defModel.get(i));
				
		  
				custScrollLists.add(new JScrollPane (Custs.get(i),
			                        ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
			                        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
			   
				
				custScrollLists.get(i).setVisible(false);
				custScrollLists.get(i).setBounds(90, 100, 200, 150);
				frmCcrminitiateSale.getContentPane().add(custScrollLists.get(i));
				
				
			}
			custScrollLists.get(0).setVisible(true);
			alreadyInitialized=true;
	
		}
		if(((Reply)r).command.equals(Command.GET_MARKETING_PATTERNS)) //if GET_PRODUCTS reply is received
		{
			patternsArr =(ArrayList<MarketingPattern>) ((Reply)r).result;
			
			designPatternCombo.removeAllItems();
			
			for(int i=0;i<patternsArr.size();i++)
			{
				designPatternCombo.addItem(("#"+patternsArr.get(i).getPatternID()).toString());
				
			}

		}
		if(((Reply)r).command.equals(Command.GET_PATERNED_CLIENTS)) //if GET_PRODUCTS reply is received
		{
			customersArr =(ArrayList<Customer>) ((Reply)r).result;
			
			for(int j=0;j<salesPersonToCustomer.size();j++)
			{
				salesPersonToCustomer.get(j).clear();
				defModel.get(j).removeAllElements();
			}
			
			for(int i=0;i<customersArr.size();i++)
			{
				for(int j=0;j<salesPersonToCustomer.size();j++)
				{
					salesPersonToCustomer.get(j).add(new CheckBoxObject((customersArr.get(i).getId() + " " +customersArr.get(i).getFirstName()+ " " +customersArr.get(i).getLastName()).toString(),0));
					
				}
		
			}
		for(int j=0;j<salesPersonToCustomer.size();j++)
		{	
		 for (int i=0;i<salesPersonToCustomer.get(j).size();i++)
			 {
			        defModel.get(j).addElement (salesPersonToCustomer.get(j).get(i).getName());
			      
			 }
		}
			
		}
		
		
	}
}


